package com.wsc.wisdomchinaqinghe.activitity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.wsc.wisdomchinaqinghe.R;
import com.wsc.wisdomchinaqinghe.Tools.JsonTools;
import com.wsc.wisdomchinaqinghe.activitity.baseactivity.BaseActivity;
import com.wsc.wisdomchinaqinghe.callback.ContributorView;
import com.wsc.wisdomchinaqinghe.javaBean.RespObj;
import com.wsc.wisdomchinaqinghe.presenter.ContributorPresenter;
import com.wsc.wisdomchinaqinghe.view.MyToast;

public class SMSActivity extends BaseActivity implements View.OnClickListener,ContributorView {
    private Button get_vertify_code, btn_next;
    private EditText edit_regist_phone_number, sms_code;
    private TextView tv_headtitle;
    private ImageView regist_back;
    private Context context;
    private MyToast myToast;
    private TimeCount registerCheck;
    private ContributorPresenter contributorPresenter;
    private final String USER_SEND_VCODE="USER_SEND_VCODE";
    private final String USER_JUDGE_VCODE="USER_JUDGE_VCODE";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.regist_layout);
        initView();
        context=this;
        myToast=new MyToast(context);
        registerCheck = new TimeCount(60000, 1000);
        contributorPresenter = new ContributorPresenter(this, context);
    }

    private void initView() {
        regist_back = (ImageView) findViewById(R.id.regist_back);
        get_vertify_code = (Button) findViewById(R.id.get_vertify_code);
        btn_next = (Button) findViewById(R.id.btn_next);
        tv_headtitle=(TextView) findViewById(R.id.tv_headtitle);
        tv_headtitle.setText(getResources().getString(R.string.regist));
        edit_regist_phone_number = (EditText) findViewById(R.id.edit_regist_phone_number);
        sms_code = (EditText) findViewById(R.id.sms_code);
        regist_back.setOnClickListener(this);
        get_vertify_code.setOnClickListener(this);
        btn_next.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.regist_back:
                finish();
                break;
            case R.id.btn_next:
                String phone=edit_regist_phone_number.getText().toString().trim();
                String vcode=sms_code.getText().toString().trim();
                if(phone.length()>0 && vcode.length()>0)
                {
                    contributorPresenter.getUserJudgeVcode(phone,vcode,USER_JUDGE_VCODE);
                }
                break;
            case R.id.get_vertify_code:
                String phoneNumber=edit_regist_phone_number.getText().toString().trim();
                  /*
                获取验证码
                 */
                if (phoneNumber.isEmpty()){
                    myToast.showShort("输入不能为空");
                }else {
                    btSendCode(phoneNumber);
                    sms_code.addTextChangedListener(new TextWatcher() {
                        @Override
                        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                        }

                        @Override
                        public void onTextChanged(CharSequence s, int start, int before, int count) {
                            btn_next.setEnabled(true);
                            btn_next.setBackgroundResource(R.drawable.ok_shape);
                        }

                        @Override
                        public void afterTextChanged(Editable editable) {

                        }
                    });
                }
                break;
        }
    }
    //发送验证码
    private void btSendCode(String number) {
        registerCheck.start();
        contributorPresenter.getUserSendVcode(number,0,USER_SEND_VCODE);
    }

    @Override
    public void onLoadContributorStart(String flag) {

    }

    @Override
    public void onLoadContributorSuccess(String jsonString, String flag) {
        try{
            //发送验证码
            if (flag.equals(USER_SEND_VCODE))
            {
                RespObj respObj = JsonTools.jsonStringToObject(jsonString, RespObj.class);
                Message msg = new Message();
                if (respObj.getCode().equals("200")) {
                    if (respObj.getMessage().equals("21")){
                        msg.what =200;
                    }
                } else if(respObj.getCode().equals("10400")){
                    msg.what =10400;
                } else if (respObj.getCode().equals("10502")) {
                    msg.what =10502;
                } else if (respObj.getCode().equals("10600")) {
                    msg.what =10600;
                } else if (respObj.getCode().equals("10601")) {
                    msg.what =10601;
                } else if (respObj.getCode().equals("10700")) {
                    msg.what =10700;
                }
                myHandler.sendMessage(msg);
            }
            //验证验证码
            else if(flag.equals(USER_JUDGE_VCODE))
            {
                RespObj respObj = JsonTools.jsonStringToObject(jsonString, RespObj.class);
                Message msg = new Message();
                if (respObj.getCode().equals("200")) {
                    if (respObj.getMessage().equals("22")){
                        msg.what =20022;
                    }
                } else if (respObj.getCode().equals("10602")) {
                    msg.what =10602;
                } else if (respObj.getCode().equals("10700")) {
                    msg.what =10700;
                }else
                {
                    myToast.showShort("请填写正确验证码!");
                }
                myHandler.sendMessage(msg);
            }
        }catch (Exception e)
        {

        }
    }

    @Override
    public void onLoadContributorComplete(String flag) {
    }

    @Override
    public void onLoadContributorError(String error,String flag) {

    }

    private Handler myHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 200:
                    myToast.showShort("发送成功,稍等...");
                    break;
                case 10400:
                    myToast.showShort("用户不存在");
                    break;
                case 10502:
                    myToast.showShort("该手机号已经注册过了");
                    break;
                case 10600:
                    myToast.showShort("验证码发送失败");
                    break;
                case 10601:
                    myToast.showShort("验证码发送频繁");
                    break;
                case 10602:
                    myToast.showShort("验证码失效");
                    break;
                case 10700:
                    myToast.showShort("操作失败");
                    break;
                case 20022:
                    myToast.showShort("验证成功");
                    Intent RegistActivity_intent = new Intent(SMSActivity.this, RegistActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putString("phone_num", edit_regist_phone_number.getText().toString().trim());
                    RegistActivity_intent.putExtras(bundle);
                    startActivity(RegistActivity_intent);
//                    finish();
                    break;
            }
            super.handleMessage(msg);
        }
    };

    /* 定义一个倒计时的内部类 */
    class TimeCount extends CountDownTimer {

        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);// 参数依次为总时长,和计时的时间间隔
        }

        @Override
        public void onFinish() {// 计时完毕时触发
            get_vertify_code.setText("重获验证码");
            setSendRegisterCodeEnable();
        }

        @Override
        public void onTick(long millisUntilFinished) {// 计时过程显示
            get_vertify_code.setText(millisUntilFinished / 1000 + "秒后重发");
            setSendRegisterCodeUnenable();
        }
    }
    private void setSendRegisterCodeEnable() {
        get_vertify_code.setClickable(true);
        get_vertify_code.setBackgroundResource(R.drawable.vertify_shape);
    }
    private void setSendRegisterCodeUnenable() {
        get_vertify_code.setClickable(false);
        get_vertify_code.setBackgroundResource(R.drawable.vertify_shape_click);
    }
}
